package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.query.BidRequestQueryObject;
import cn.wolfcode.p2p.bussness.query.PaymentScheduleQueryObject;
import cn.wolfcode.p2p.bussness.service.IBidRequestService;
import cn.wolfcode.p2p.bussness.service.IExpAccountService;
import cn.wolfcode.p2p.bussness.service.IPaymentScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExpBidRequestPublishController {

    @Autowired
    private IExpAccountService expAccountService;

    @Autowired
    private IBidRequestService bidRequestService;

    @Autowired
    private IPaymentScheduleService paymentScheduleService;

    @RequestMapping("/exp_list")
    public String expPublish(Model model) {
        //最小体验金 借款金额
        model.addAttribute("minBidRequestAmount", Constants.BORROW_AMOUNT_MIN);
        //最小投标金额
        model.addAttribute("minBidAmount", Constants.BID_AMOUNT_MIN);
        return "expbidrequest/expbidrequestpublish";
    }

    /**
     * 发布体验标
     *
     * @param bidRequest 借款对象
     * @return
     */
    @RequestMapping("/expBidRequestPublish")
    @ResponseBody
    public AjaxResult expBidRequestPublish(BidRequest bidRequest) {
        AjaxResult result = null;
        try {
            bidRequestService.applyExp(bidRequest);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    /**
     * 发标列表
     */
    @RequestMapping("/expBidRequest_list")
    public String expBidRequestList(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
        qo.setBidRequestType(Constants.BIDREQUEST_TYPE_EXP);
        model.addAttribute("pageInfo", bidRequestService.queryList(qo));
        return "expbidrequest/expbidrequestlist";
    }

    /**
     * 体验标还款列表
     */
    @RequestMapping("/expBidRequestReturn_list")
    public String expBidRequestReturnList(@ModelAttribute("qo") PaymentScheduleQueryObject qo, Model model) {
        //设置体验标
        qo.setBidRequestType(Constants.BIDREQUEST_TYPE_EXP);
        model.addAttribute("pageInfo", paymentScheduleService.queryPage(qo));
        return "expbidrequest/expbidrequestreturnlist";
    }

    /**
     * 体验标还款操作
     * @param id    还款对象ID
     * @return
     */
    @RequestMapping("/returnMoney")
    @ResponseBody
    public AjaxResult returnMoney(Long id) {
        AjaxResult result = null;
        try {
            //后台还款代码
            paymentScheduleService.returnMoney(id);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
